package com.siyu.tickets.action;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siyu.common.exception.ExceptionHandler;
import com.siyu.common.util.StrUtils;
import com.siyu.tickets.entity.Ticket;
import com.siyu.tickets.manager.TicketMng;
import com.siyu.tickets.web.SysUtils;

/**
 * 电子票相关
 * 
 * @since 2013-7-15
 * @version 0.1
 * @author cuigx
 */
@Controller
public class TicketAct {

	private static final Logger log = LoggerFactory.getLogger(TicketAct.class);

	@RequestMapping(value = "/ticket/validate", method = RequestMethod.GET)
	public String validate(HttpServletRequest request, ModelMap model) {
		return "ticket/validate";
	}

	@RequestMapping(value = "/ticket/validate", method = RequestMethod.POST)
	@ResponseBody
	public String validateSubmit(String code, Integer count, HttpServletRequest request, ModelMap model)
			throws JSONException {
		JSONObject obj = new JSONObject();
		obj.put("code", StrUtils.html(code));
		obj.put("count", count);
		if (count <= 0) {
			obj.put("result", false);
			obj.put("message", "验证数量必须大于0");
			return obj.toString();
		}
		try {
			Integer userId = SysUtils.getUserId(request);
			Ticket ticket = manager.validate(userId, code, count);
			obj.put("result", true);
			obj.put("order", ticket.getOrder().getId());
			obj.put("total", ticket.getOrder().getQuantity());
			obj.put("remain", ticket.getOrder().getQuantity() - ticket.getVerifyCount());
			obj.put("time", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(ticket.getConsumeTime()));
		} catch (Exception e) {
			ExceptionHandler.handle(e);
			obj.put("result", false);
			obj.put("message", e.getMessage());
		}
		return obj.toString();
	}

	@RequestMapping("/ticket/query")
	@ResponseBody
	public String query(String code, HttpServletRequest request, ModelMap model) throws JSONException {
		JSONObject obj = new JSONObject();
		Ticket ticket = manager.findByCode(code);
		obj.put("code", StrUtils.html(code));

		if (ticket == null) {
			obj.put("result", false);
			obj.put("message", "无效的电子码");
		} else {
			obj.put("result", true);
			obj.put("order", ticket.getOrder().getId());
			obj.put("total", ticket.getOrder().getQuantity());
			obj.put("count", ticket.getVerifyCount());
			obj.put("remain", ticket.getOrder().getQuantity() - ticket.getVerifyCount());
			if (ticket.getConsumeTime() != null) {
				obj.put("time", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(ticket.getConsumeTime()));
			}
		}
		return obj.toString();
	}

	@Autowired
	private TicketMng manager;
}
